<template>
  <div class="topbar">
    <div class="wrap topbar-wrap">
      <div class="left">欢迎来到叩丁狼积分商城</div>
      <ul class="right">
        <li>
          <img :src="userInfo.headImg" width="24px" alt="" />
          用户名：{{ userInfo.nickName }}
        </li>
        <li>我的积分：{{ userInfo.coin }}</li>
        <li>获取积分</li>
        <li>叩丁狼官网</li>
        <li class="cart-btn" v-if="ishowLogin">
          <img src="../assets/img/cart.png" alt="" />
          <span>购物车</span>
          <strong>{{ cartTotal }}</strong>
        </li>
        <li class="current" @click="changeIsShowLoginModel(true)" v-else>
          登录
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
import { mapMutations, mapState, mapActions } from "vuex";
import { WxLoginAPI } from "@/request/api";
// import { ishowLogin } from "@/store/loginStatus/index";
export default {
  data() {
    return {};
  },

  computed: {
    ...mapState({
      ishowLogin: (state) => state.LoginStatus.ishowLogin,
      cartTotal: (state) => state.UserInfo.cartTotal,
      userInfo: (state) => state.UserInfo.userInfo,
    }),
  },

  methods: {
    ...mapMutations({
      changeIsShowLoginModel: "showModel/changeIsShowLoginModel",
      changeIshowLogin: "LoginStatus/changeIshowLogin",
      initUser: "UserInfo/initUser",
    }),
    ...mapActions({
      asyncchangeisShowToast: "showToast/asyncchangeisShowToast",
      asyupdateUser: "UserInfo/asyupdateUser",
    }),
  },
  async created() {
    // console.log(this.$route.query.code);
    let code = this.$route.query.code;
    if (code) {
      let res = await WxLoginAPI({
        code,
      });
      // console.log("res55", res);

      if (res.data.code == 0) {
        // 提示框
        // this.$router.onReady(() => {
        //   console.log(111);
        // });

        this.asyncchangeisShowToast({
          isShowToast: true,
          msg: "扫码登录成功",
          type: "success",
        });
        // 存touken
        localStorage.setItem("x-auth-token", res.data["x-auth-token"]);
        // 改变登陆框
        this.changeIshowLogin(true);
        // 路由跳转
        this.$router.push(this.$route.path);

        this.asyupdateUser();
      } else if (res.data.code == 400) {
        this.asyncchangeisShowToast({
          isShowToast: true,
          msg: "微信扫码过期，请重新扫码",
          type: "danger",
        });
        setTimeout(() => {
          this.changeIsShowLoginModel(true);
        }, 1500);
      } else if (res.data.code == 407) {
        this.asyncchangeisShowToast({
          isShowToast: true,
          msg: "狗比绑定手机号",
          type: "qita",
        });
        setTimeout(() => {
          this.changeIsShowLoginModel(true);
        }, 1500);
        localStorage.setItem("uuid", res.data.uuid);
      }
    } else {
      let token = localStorage.getItem("x-auth-token");
      this.changeIshowLogin(Boolean(token));
      if (token) {
        this.asyupdateUser();
      } else {
        this.initUser();
      }
    }
  },
  // watch: {
  //   "$route.path": {
  //     handler(newVal, oldVal) {
  //       // console.log("watch", newVal, oldVal);
  //       let token = localStorage.getItem("x-auth-token");

  //       // if (token) {
  //       //   this.changeIshowLogin(true);
  //       // } else {
  //       //   this.changeIshowLogin(false);
  //       // }

  //       this.changeIshowLogin(Boolean(token));
  //     },
  //   },
  // },
};
</script>

<style lang="less" scoped>
.topbar {
  height: 40px;
  background: #242b39;
  line-height: 40px;
  .topbar-wrap {
    display: flex;
    justify-content: space-between;
    font-size: 14px;
    font-family: SourceHanSansSC;
    font-weight: 300;
    color: #fffefe;
    align-items: center;
    .right {
      display: flex;
      justify-content: space-between;
      li {
        margin-left: 15px;
        cursor: pointer;
        &.current {
          width: 124px;
          height: 40px;
          background: #0a328e;
          text-align: center;
        }
      }
    }
    img {
      border-radius: 13px;
      vertical-align: middle;
    }
  }
}

.cart-btn {
  width: 124px;
  height: 40px;
  background: #0a328e;
  text-align: center;
  img {
    width: 20px;
    height: 19px;
    vertical-align: middle;
  }
  span {
    margin: 0 8px;
  }
  strong {
    width: 22px;
    height: 22px;
    line-height: 22px;
    display: inline-block;
    border-radius: 50%;
    background-color: #fd604d;
  }
}
</style>